package us.icitap.cm.ui.policecase;

import java.util.List;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.Officer;
import us.icitap.cm.business.entities.Place;
import us.icitap.cm.business.entities.regmin.Cities;
import us.icitap.cm.business.entities.regmin.District;
import us.icitap.cm.business.entities.regmin.Region;
import us.icitap.cm.business.entities.regmin.States;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustTextArea;
import us.icitap.cm.ui.custom.CustTextField;
import us.icitap.cm.utils.ProjectLists;
import us.icitap.utils.Result;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

public class PlaceField extends CustomField<Place>{
	
	private final CustComboBox streetSelect = new CustComboBox("Rruga");
	private final CustTextField numberTextField = new CustTextField("Numri");
	private final CustTextField apartmentTextField = new CustTextField("Apartamenti");
	private final CustComboBox stateSelect = new CustComboBox("Shteti");
	private final CustComboBox citySelect = new CustComboBox("Qyteti");
	private final CustComboBox regionSelect = new CustComboBox("Qarku");
	private final CustComboBox districtSelect = new CustComboBox("Rrethi");
	
	private final CustComboBox placeClassificationSelect = new CustComboBox("Klasifikimi i vendit/objektit");
	private final CustComboBox placeTypeSelect = new CustComboBox("Tipi i vendit/objektit");
	
	
	private final CustTextArea addressTextArea = new CustTextArea("Shenime");
	
	private final CustBeanFieldGroup<Place> binder = new CustBeanFieldGroup<Place>(Place.class);
	
	
	
	public PlaceField() {
		
		numberTextField.setWidth("6em");
		addressTextArea.setWidth("20em");
		addressTextArea.setRows(4);
		
		stateSelect.setContainerDataSource(new BeanItemContainer<States>(States.class, ProjectLists.getStatesList(false)));
		stateSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		stateSelect.setItemCaptionPropertyId("tag");
		
		citySelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		citySelect.setItemCaptionPropertyId("tag");
		
		regionSelect.setContainerDataSource(new BeanItemContainer<Region>(Region.class, ProjectLists.getRegionList(false)));
		regionSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		regionSelect.setItemCaptionPropertyId("tag");
		
		districtSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		districtSelect.setItemCaptionPropertyId("tag");
		
		placeClassificationSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.PlaceClassification, false)));
		placeClassificationSelect.setImmediate(true);
		
		placeTypeSelect.setImmediate(true);
		
		stateSelect.setImmediate(true);
		stateSelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
				citySelect.removeAllItems();
				if(stateSelect.getValue() != null){
					Result<List<Cities>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getCities(stateSelect.getValue());
					if(result.isSucceeded() && result.getObject() != null && !result.getObject().isEmpty()){
						citySelect.setContainerDataSource(new BeanItemContainer<Cities>(Cities.class, result.getObject()));
						
					}
				}
				
				
			}
		});
		
		regionSelect.setImmediate(true);
		regionSelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
				districtSelect.removeAllItems();
				if(regionSelect.getValue() != null){
					Result<List<District>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getDistrict(regionSelect.getValue());
					if(result.isSucceeded() && result.getObject() != null && !result.getObject().isEmpty()){
						districtSelect.setContainerDataSource(new BeanItemContainer<District>(District.class, result.getObject()));
						
					}
				}
				
			}
		});
		
		citySelect.setImmediate(true);
		citySelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
				if(citySelect.getValue() != null){
					Cities city = (Cities) citySelect.getValue();
					if(city.getDistrict()!= null){
						regionSelect.setValue(city.getDistrict().getRegion());
						districtSelect.setValue(city.getDistrict());
						addressTextArea.focus();
					}
				}
				
			}
		});
		
		
		
		
		placeClassificationSelect.addValueChangeListener(new ValueChangeListener() {
			

			@Override
			public void valueChange(
					com.vaadin.data.Property.ValueChangeEvent event) {
				placeTypeSelect.removeAllItems();
				if(placeClassificationSelect.getValue() != null){
					AppList parent = (AppList) placeClassificationSelect.getValue();
					Result<List<AppList>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getAppListChildren(parent);
					if(result.isSucceeded() && result.getObject() != null && !result.getObject().isEmpty()){
						placeTypeSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, result.getObject()));
						
					}
				}
				
			}
		});
		
		
		binder.bind(placeClassificationSelect, "placeClassification");
		binder.bind(placeTypeSelect, "placeType");
		binder.bind(streetSelect, "street");
		binder.bind(numberTextField, "number");
		binder.bind(apartmentTextField, "apartment");
		binder.bind(stateSelect, "states");
		binder.bind(citySelect, "cities");
		binder.bind(districtSelect, "district");
		binder.bind(regionSelect, "region");
		binder.bind(addressTextArea, "addressText");
	}



	@Override
	protected Component initContent() {
		VerticalLayout vl = new VerticalLayout();
		FormLayout fl = new FormLayout();
		fl.addStyleName("cmLayout");
		fl.setMargin(true);
		
		fl.addComponent(this.placeClassificationSelect);
		fl.addComponent(this.placeTypeSelect);
		fl.addComponent(this.streetSelect);
		fl.addComponent(this.numberTextField);
		fl.addComponent(this.apartmentTextField);
		fl.addComponent(this.stateSelect);
		fl.addComponent(this.citySelect);
		fl.addComponent(this.regionSelect);
		fl.addComponent(this.districtSelect);
		fl.addComponent(this.addressTextArea);
		
		vl.addComponent(fl);
		return vl;
	}



	@Override
	public Class<? extends Place> getType() {
		// TODO Auto-generated method stub
		return Place.class;
	}
	
	@Override
	protected void setInternalValue(Place newValue) {
		System.out.println("PlaceField.setInternalValue()");
		System.out.println(newValue);
		System.out.println(newValue.getAppEntity().getEntityType().toString());
		super.setInternalValue(newValue);
		binder.setItemDataSource(newValue);
	}



	@Override
	public void commit() throws SourceException, InvalidValueException {
		try {
			System.out.println("PlaceField.commit()");
			binder.commit();
			System.out.println(this.getValue().toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		super.commit();
	}
	
	
	
	
}
